
*****************************************
*****************************************
****Presidential Campaigns and the Fundamentals Reconsidered
****Peter K. Enns and Brian Richman
****Journal of Politics
****Replication: CA Treatment Effect
****		     Placebo Analysis
*****************************************
*****************************************

***Set Working Directory"
cd ""

*****************
*****SET UP******
***************** ;


*Change delimiter to be ";" ;
	#d ;

*Clear all existing data ;
	clear ;

*Set more to off ;
	set more off ;

*Install coarsened matching program ;
	/* Matthew Blackwell, Stefano M. Iacus, Gary King, and Giuseppe Porro (2009), "cem: Coarsened exact matching in Stata", The Stata Journal, 
		9(4), 524-546, copy at <http://gking.harvard.edu/files/cemStata.pdf> */ ;
*Only needs to be installed once ;
*To disable, add "*" in front of lines ;
	*ssc install cem ;

	set mem 1000000k ;

use "EnnsRichman_NAES_VoteIntentions.dta", clear ;
	
************************************************************** ;
**Change delimeter to be end of line ;
#d cr
**************************************************************

************************************************************************************************************
**Create time windows variable
************************************************************************************************************

gen windows = .
	replace windows = -1 if days_elec <= -1 & days_elec >= -21
	replace windows = -2 if days_elec <= -22 & days_elec >= -39
	replace windows = -3 if days_elec <= -40 & days_elec >= -60
	replace windows = -4 if days_elec <= -61 & days_elec >= -80
	replace windows = -5 if days_elec <= -81 & days_elec >= -100
************************************************************************************************************


 	
*******************************************************************
**Create dummy variable for each state 

foreach x in AL	AR	AZ	CA	CO	CT	DC	DE	FL	GA	IA	ID	IL	IN	KS	KY	LA	MA	MD	ME	MI	MN	MO	MS	MT	NC	ND	NE	NH	NJ	NM	NV	NY	OH	OK	OR	PA	RI	SC	SD	TN	TX	UT	VA	VT	WA	WI	WV	WY {

gen `x' = (cst == "`x'")
}
*******************************************************************


************************
*****SETUP ANALYSIS*****
************************ ;



******************************************************************
**Identify which states will be used for placebo test
**States with >= 1000 observations in the NAES 2000 dataset are included
local placebos = "NY	FL	PA	OH	IL	MI	NC VA	GA	NJ	TN	WI	MA IN	WA	MO	MN	MD	AZ	AL TX"
******************************************************************

*********************************************************************
**Keep variables that are needed for analysis
keep registered CA `placebos' zeduc zexp10 zknow18 windows correct_pv15 cst ztot_presad_exp3 noaddata
**********************************************************************

***************************************************************************
**Create temporary datafile to use for analysis named "match_setup"
**"match_setup" has all of data setup and will be called to begin each calculation below

*Declare name of tempfile
tempfile match_setup

*Save tempfile
save `match_setup', replace
******************************************************************************



*******************************************************************************
**Create dataset where results will be stored (using "postfile" commands)

**Closes postfile dataset if dataset is already open
capture postclose results


**Creates dataset using postfile command
postfile results windows str2(state) estimate se matched unmatched treated using "CAplacebo.dta", replace
*********************************************************************************


*********************************************************************************
**Analysis for CA
***Loop by window (e.g., window 1 = days -1 to -20)
forval i = 1/5 {



	**Conduct calculation for CA
	foreach x in CA {


		**Clear all data
		clear
	
		**Use tempfile with all data setup
		use `match_setup'

		**Keep only registered voters
		keep if registered == 1

		**Use only one time window at a time
		keep if windows == -`i'
	
		**Conduct coarsened match (See "help cem" for more details on program)
		**Description of coarsening:
			**  #0 = do not coarsen.  Exact values matched.
			**  #n = divide variable into n equally spaced cutpoints

			cem zknow18 zeduc zexp10 ztot_presad_exp3 noaddata(#0), treatment(`x')			


		**Collect statistics on number of observations matched, unmatched, and treated 
			**Cross tabulation of treatment and matched status (as matrix)
			matrix A = r(match_table)

			**Read data from cross tab matrix
			local matched = r(n_matched)
			local unmatched = A[3,1]
			local treated = A[1,2]
		


		
	
	
		**Estimate the sample average treatment effect on the treated (SATT) 
		** Because some variables are coarsened, some imbalance will remain.
		** We therefore include control variables in the regression model to control for remaining imbalance (Blackwell, Iacus, King, and Porro 2009, p.537)
		** "new_weight" = the weight assigned to the observation's stratum. Equals 0 if the observation is unmatched and 1 if 
			**the observation is treated.

 		xi: logit correct_pv15 `x' zeduc zknow18 zexp10 ztot_presad_exp3 noaddata [iweight=cem_weights]
					

		**Add results data to dataset created using postfile
		post results (-`i') ("`x'") (_b[`x']) (_se[`x']) (`matched') (`unmatched') (`treated') 
	
		**Clear all data
		clear


	**End loop for CA
	}
	

**End loop by window
}
*********************************************************************************

*********************************************************************************
**Analysis for Placebo states
**Loop by window (e.g., window 1 = days -1 to -20)
forval i = 1/5 {

	**Also loop by each placebo state
	foreach x of local placebos {

		**Clear all data
		clear
	
		**Use tempfile with all data setup
		use `match_setup'
	
		**Drop treated state (i.e., CA) from sample
		drop if cst == "CA"

		**Use only registered voters
		keep if registered == 1

		**Use only one time window at a time
		keep if windows == -`i'
	
		**Conduct coarsened match (See "help cem" for more details on program)
		**Description of coarsening:
			**  #0 = do not coarsen.  Exact values matched.
			**  #n = divide variable into n` equally spaced cutpoints
		cem zknow18 zeduc zexp10 ztot_presad_exp3 noaddata(#0), treatment(`x')

			**Cross tabulation of treatment and matched status (as matrix)
			matrix A = r(match_table)

			**Read data from cross tab matrix
			local matched = r(n_matched)
			local unmatched = A[3,1]
			local treated = A[1,2]


		
		
		

	
		**Estimate the sample average treatment effect on the treated (SATT) 
		** Because some variables are coarsened, some imbalance will remain.
		** We therefore include control variables in the regression model to control for remaining imbalance (Blackwell, Iacus, King, and Porro 2009, p.537)

		xi: logit correct_pv15 `x' zeduc zknow18 zexp10 ztot_presad_exp3 noaddata [iweight=cem_weights]

	
	
		**Calculate Standard Deviation of each estimate

		**Add results data to dataset created using postfile
		post results (-`i') ("`x'") (_b[`x']) (_se[`x']) (`matched') (`unmatched') (`treated')
	
		**Clear all data
		clear

	**End loop for each placebo state
	}
	

**End loop by window
}

*********************************************************************************

 ********************************************
**Close postfile dataset
postclose results
********************************************


*********************************
*****SETUP TO GRAPH RESULTS*****
********************************* ;
 *************************************************************
**Open results dataset
use "CAplacebo.dta"
**************************************************************




********************************************************
**Sort by state and time window
sort state windows
*********************************************************

****************************************************
**Set color scheme for graphs

set scheme s1mono
****************************************************

***********************
*****GRAPH RESULTS*****
*********************** ;



**Calculate t-statistic for EACH TIME PERIOD 
gen tstat = .
	replace tstat = (estimate[_n]/se[_n]) if windows[_n] == -5
	replace tstat = (estimate[_n]/se[_n]) if windows[_n] == -4
	replace tstat = (estimate[_n]/se[_n]) if windows[_n] == -3
	replace tstat = (estimate[_n]/se[_n]) if windows[_n] == -2
	replace tstat = (estimate[_n]/se[_n]) if windows[_n] == -1


***********************************************************
*Create graph with CA and all placebo states: TEST STATISTICS
graph twoway line tstat windows if state == "FL" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "TX" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "IL" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "MI" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "NC" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "NY" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "OH" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "PA" & windows>=-5, lcolor(gs10)  ||  line tstat windows if state == "TX" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "VA" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "GA" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "NJ" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "TN" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "WI" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "IN" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "WA" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "MO" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "MN" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "MA" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "MD" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "AZ" & windows>=-5, lcolor(gs10) ||  line tstat windows if state == "AL" & windows>=-5, lcolor(gs10) || line tstat windows if state == "CA" & windows>=-5, ylabel(-2 "2" 0 "0" 2 "2") yscale(range(-3 3) axis(1)) lcolor(black) lwidth(*2) yline(0, lstyle(foreground)) ttext(2.5 -2 "CA", orientation(horizontal)) legend(off) xlabel(-.5 "0" -1.5 "-20" -2.5 "-40" -3.5 "-60" -4.5 "-80" -5.5 "-100") xtitle("Days Before Election")  yline(1.96, lpattern(dash) lwidth(thick)) yline(-1.96, lpattern(dash) lwidth(thick)) ytitle("t-value for State Dummy")
************************************************************
